$(document).ready(function() {
	$.ajax({
		url:BASE_URL_KETTLE_SLAVE_SERVER_CRUD,
		type:'get',
		async: false,
		dataType:'json',
		contentType:"application/json",
		success:function(data){
			$.each(data._embedded.kettleSlaveServers, function(i, item){
				var url = BASE_URL_KETTLE_SLAVE_SERVER_CRUD +'/'+item.id;
				if( i == "0")
				{
					getServiceInfo(BASE_URL_KETTLE_SLAVE_SERVER_CRUD +'/'+item.id);
					html = '<li><a href="javascript:void(0)" data-url="' + url + '" class="getServiceInfo active"><img id='+item.id+' src="../../../../images/icons/connectIcon.png" class="fl">'+item.name+'</a>'+
				        '</li>';
				}else
				{
					html = '<li><a href="javascript:void(0)" data-url="' + url + '" class="getServiceInfo"><img id='+item.id+' src="../../../../images/icons/connectIcon.png" class="fl">'+item.name+'</a>'+
				        '</li>';
				}
				$(".kettle_tree").append(html);

			});		
		}
	});
	setInterval(checkNodeIsRunning,10000);	 
});


function checkNodeIsRunning(){
	$.ajax({
		url:BASE_URL_KETTLE_SLAVE_SERVER +'/runningStatus',
		type:'get',
		async: false,
		dataType:'json',
		contentType:"application/json",
		success:function(data){
			var UnRunning = data.isNotRuning;
			for(var i = 0;i < UnRunning.length;i++){
				$('#'+UnRunning[i]).attr('src','../../../../images/icons/CrashIcon.png');
			}
			
			var Running = data.isRuning;
			for(var i = 0;i < Running.length;i++){
				$('#'+Running[i]).attr('src','../../../../images/icons/connectIcon.png');
			}
		}
	});
	
}
$("body").delegate(".getServiceInfo","click",function(e){
	var url = $(this).data("url");
	$(".getServiceInfo").removeClass("active");
	$(this).addClass("active");
	getServiceInfo(url);
});

function getServiceInfo(url){
	
	$.ajax({
		url:url,
		type:'get',
		dataType:'json',
		contentType:"application/json",
		success:function(data){	
			alert(JSON.stringify(data));
			$("#ServerName").html(data.name);
			if(data.isMaster){
				$("#ServerType").html("主服务器");
			}else{
				$("#ServerType").html("从服务器");
			}
			$("#ServerIpAddress").html(data.hostName);
			//$("#ServerPort").html("8082");
			var connectUrl = "http://"+data.hostName+":"+data.port+"/myHandler";
			ServiceInformationDisplay(connectUrl); 
		}
	});
}

function ServiceInformationDisplay(connectUrl){
		var sock = SockJsCreator(connectUrl)
		sock.onopen = function() {
		     sock.send('test');
		 };
		 sock.onmessage = function(e) {
			 var Data = JSON.parse(e.data); 
			 //alert("json:"+Data.cpu.totalPerc);
			 var series = chart.series[0];
			 var series1 = chart1.series[0];
			 var x = (new Date()).getTime(),
	         // current time         
	         y = Math.random();
	         series.addPoint([x, Data.cpu.totalPerc], true, true);
	         series1.addPoint([x,Data.memory.used],true,true);
		     //console.log('message', e.data);
		 };
		 sock.onclose = function() {
		     //console.log('close');
		 };
	
	
	    Highcharts.setOptions({
	        global: {
	            useUTC: false
	        }
	    });
	    
}
//cpu使用率曲线图
var chart;
chart = new Highcharts.Chart({
	chart: {
		renderTo: 'container',
		type: 'spline',
		animation: Highcharts.svg,
		// don't animate in old IE               
		marginRight: 10,
		events: {
			load: function() {}
		}
	},
	title: {
		text: 'CPU',
		x: 0 //center
	},
	subtitle: {
		/* text: '平均值：5.6%',
	            x: 0*/
	},
	credits: {
		enabled: false
	},
	xAxis: {
		type: 'datetime',
		tickPixelInterval: 150
	},
	yAxis: [{
		title: {
			text: '使用率 (%)'
		},
		plotLines: [{
			value: 0,
			width: 1,
			color: '#808080'
		}]
	}],
	tooltip: {
		formatter: function() {
			return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 2);
		}
	},
	legend: {
		enabled: false
	},
	exporting: {
		enabled: false
	},
	series: [{
		name: 'CPU使用率',
		data: (function() { // generate an array of random data                             
			var data = [],
			time = (new Date()).getTime(),
			i;
			for (i = -19; i <= 0; i++) {
				data.push({
					x: time + i * 1000,
					y: 1
				});
			}
			return data;
		})()
	}]
}); 


//内存使用率曲线图
var chart1;
chart1 = new Highcharts.Chart({
	chart: {
		renderTo: 'chart2',
		type: 'spline',
		animation: Highcharts.svg,
		// don't animate in old IE               
		marginRight: 10,
		events: {
			load: function() {}
		}
	},
	title: {
		text: '物理内存',
		x: 0 //center
	},
	subtitle: {
		/*text: '平均值：2201.11(MB)',
		        x: 0*/
	},
	credits: {
		enabled: false
	},
	xAxis: {
		type: 'datetime',
		tickPixelInterval: 150
	},
	yAxis: [{
		title: {
			text: '使用内存 (MB)'
		},
		plotLines: [{
			value: 0,
			width: 1,
			color: '#808080'
		}]
	}],
	tooltip: {
		formatter: function() {
			return '<b>使用内存：</b>'+
			this.y + 'MB';
			
		}
	},
	legend: {
		enabled: false
	},
	exporting: {
		enabled: false
	},
	series: [{
		name: 'Random data',
		data: (function() { // generate an array of random data                             
			var data = [],
			time = (new Date()).getTime(),
			i;
			for (i = -19; i <= 0; i++) {
				data.push({
					x: time + i * 1000,
					y: 1024
				});
			}
			return data;
		})()
	}]
});